Modul 6 von 15 · 📖 4 min Lesezeit · ⏱ 30 min gesamt

FI-AE 07 Objektorientierte Programmierung (EN)

Inhaltsverzeichnis (6 Abschnitte)
  1. Concepts and Background
  2. Architecture Diagram
  3. Practical Steps
  4. Common Pitfalls
  5. Further Resources
  6. Knowledge Check

FI-AE 07 Object-Oriented Programming

Object-oriented programming (OOP) forms the foundation of modern software development. In this module, you will deepen your understanding of the core concepts of OOP – encapsulation, inheritance, and polymorphism – and learn to apply them to design robust and maintainable software architectures. Through a comprehensive example, you will develop practical skills in applying the SOLID principles, which form the basis for professional, extensible code design.

Concepts and Background

Encapsulation
Encapsulation hides the internal states and implementations of an object and makes them accessible only through defined interfaces (methods). It protects data from unauthorized access and reduces dependencies between components.
Inheritance
Inheritance enables the creation of new classes (derived classes) based on existing classes (base classes). The derived class inherits properties and methods from the base class and can extend or override them.
Polymorphism
Polymorphism ("many forms") allows objects of different classes to be treated through a common interface. Methods can be implemented differently depending on the object type, which is resolved dynamically at runtime.
SOLID Principles
The SOLID principles are five design guidelines for object-oriented software: Single Responsibility Principle, Open/Closed Principle, Liskov Substitution Principle, Interface Segregation Principle, and Dependency Inversion Principle. They ensure flexible, maintainable, and extensible systems.

Architecture Diagram

classDiagram
  class Fahrzeug {
    +geschwindigkeit: int
    +position: Punkt
    +beschleune(delta: int)
    +bremsen(delta: int)
    +getPosition(): Punkt
  }
  
  class Punkt {
    +x: int
    +y: int
    +verschiebe(dx: int, dy: int)
  }
  
  class Auto extends Fahrzeug {
    +anzahlTueren: int
    +oeffneTuer(tuerNr: int)
  }
  
  class Motorrad extends Fahrzeug {
    +hatBeifahrer: boolean
    +setzeBeifahrer(status: boolean)
  }
  
  Fahrzeug "1" *-- "1" Punkt
  Fahrzeug <|-- Auto
  Fahrzeug <|-- Motorrad

Practical Steps

  1. Define a base class Fahrzeug with common attributes such as geschwindigkeit and position as well as methods for speed control. This lays the foundation for later inheritance.
  2. Implement encapsulation through access modifiers (e.g., private for internal data and public for methods) to prevent direct access to object states.
  3. Derive specific classes like Auto and Motorrad from the base class Fahrzeug and add class-specific attributes and methods.
  4. Override methods of the base class in the derived classes to implement polymorphic behavior – for example, different implementations of the beschleune method.
  5. Apply the Single Responsibility Principle by creating separate classes for different responsibilities (e.g., Punkt for position data).
  6. Implement the Open/Closed Principle by designing the base class so that it can be extended but not modified.
  7. Use interfaces to satisfy the Interface Segregation Principle – for example, an IFahrzeug interface with only the necessary methods.
  8. Apply Dependency Injection to decouple from concrete implementations and realize the Dependency Inversion Principle.

Common Pitfalls

Further Resources

Knowledge Check

Four questions for self-assessment. Click on each question to see the correct answer and explanation.

What is the main purpose of encapsulation in object-oriented programming?
  • A) Maximum code reusability through inheritance
  • B) Hiding internal implementation details and protecting data
  • C) Facilitating communication between different objects
  • D) Reducing the number of classes in a system

Correct Answer: B. Encapsulation hides internal states and makes them accessible only through defined interfaces, while A and C rather relate to other OOP concepts and D is not the main goal.

Which SOLID principle states that a class should have only one reason to change?
  • A) Open/Closed Principle
  • B) Interface Segregation Principle
  • C) Single Responsibility Principle
  • D) Liskov Substitution Principle

Correct Answer: C. The Single Responsibility Principle states that a class should have only a single responsibility, while A relates to extensibility, B to specific interfaces, and D to substitutability of objects.

What does polymorphism mean in the context of object-oriented programming?
  • A) The ability of a class to inherit from another class
  • B) The ability of objects to change during runtime
  • C) The ability to have methods with the same name but different implementations
  • D) The ability to bundle data and methods in a class

Correct Answer: C. Polymorphism allows objects of different classes to be treated as objects of a common superclass, with methods being implemented differently depending on the specific object type.